/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.itgrail.pilot.app.command.cmo.query.QueryPerminByRoleCmd;
import com.itgrail.pilot.app.command.co.PermissionCO;
import com.itgrail.pilot.app.command.co.QueryPerminByRoleCO;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.PermissionMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.RolePermissionMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.PermissionDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.grail.utils.reflect.BeanMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建角色或修改角色查询对应的权限
 */
@CmdHandler
public class QueryPerminByRoleCmdExe implements CommandExecutorI<ResultData<List<QueryPerminByRoleCO>>, QueryPerminByRoleCmd> {

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public ResultData<List<QueryPerminByRoleCO>> execute(QueryPerminByRoleCmd cmd) {
        ResultData<List<QueryPerminByRoleCO>> resultData = ResultData.success(null);
        List<String> perGroupList = permissionMapper.queryPerGroup();
        if (null == perGroupList || perGroupList.size() == 0) {
            return resultData;
        }

        List<QueryPerminByRoleCO> perminByRoleCOS = new ArrayList<>();
        for (String group : perGroupList) {
            if (null == group) {
                continue;
            }
            List<PermissionDO> allPermDOList = permissionMapper.queryAllPermByGroup(group);
            if (null == allPermDOList || allPermDOList.size() == 0) {
                continue;
            }
            //新增角色查看所有列表
            if (StringUtils.isEmpty(cmd.getRoleName())) {
                AssemResult(perminByRoleCOS, group, allPermDOList);
            } else {
                QueryPerminByRoleCO queryPerminByRoleCO = new QueryPerminByRoleCO();
                List<PermissionCO> allPermisCOList = BeanMapper.mapList(allPermDOList, PermissionDO.class, PermissionCO.class);
                for (PermissionCO allPer : allPermisCOList) {
                    int count = rolePermissionMapper.countRolePermByRoleAndPid(allPer.getId(), cmd.getRoleName());
                    if (count > 0) {
                        allPer.setRelated(true);
                    }
                }
                queryPerminByRoleCO.setPermList(allPermisCOList);
                queryPerminByRoleCO.setRoleGroup(group);
                perminByRoleCOS.add(queryPerminByRoleCO);
            }
        }
        return resultData.setData(perminByRoleCOS);
    }

    private void AssemResult(List<QueryPerminByRoleCO> perminByRoleCOS, String group, List<PermissionDO> permissionDOList) {
        QueryPerminByRoleCO queryPerminByRoleCO = new QueryPerminByRoleCO();
        queryPerminByRoleCO.setPermList(BeanMapper.mapList(permissionDOList, PermissionDO.class, PermissionCO.class));
        queryPerminByRoleCO.setRoleGroup(group);
        perminByRoleCOS.add(queryPerminByRoleCO);
    }
}
